package wechat.handler.response.business;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import wechat.handler.bean.response.EventMessageBean;
import wechat.handler.bean.response.MessageBaseBean;
import wechat.handler.bean.response.event.menu.MenuScancodeWaitmsgBean;
import wechat.handler.bean.response.event.message.SubscribeEventBean;
import wechat.handler.bean.response.message.receive.ReceiveTextBean;
import wechat.handler.response.business.event.menu.MenuScancodeWaitmsgBusiness;
import wechat.handler.response.business.event.message.SubscribeEventBusiness;
import wechat.handler.response.business.message.receive.ReceiveTextBusiness;
import wechat.handler.util.WechatUtil;
import wechat.handler.util.aes.WXBizMsgCrypt;

public class WechatInformationBusiness {
	private Logger log = LoggerFactory.getLogger(WechatInformationBusiness.class);
	
	
	public WechatInformationBusiness() {

	}

	
	public String execute(String appid,String str,WXBizMsgCrypt wxBizMsgCrypt) throws Exception {
		log.info("WechatInformationBusiness --- execute appid=" + appid);
		
		MessageBaseBean messageBaseBean = new MessageBaseBean(str);
		messageBaseBean.transform();
		
		String result = "";
		
		if (appid == null) {
			result = distribute(messageBaseBean, str);
		}
		else {
			result = distribute(appid,messageBaseBean,str);
		}
		
		
		String timestamp = String.valueOf(System.currentTimeMillis());
		String nonce = WechatUtil.getNonceStr();
		String encryptMsg = wxBizMsgCrypt.encryptMsg(result, timestamp, nonce);

		log.debug("Execute -- execute   encryptMsg=" + encryptMsg);
		
		return encryptMsg;
	}
	
	
	public String execute(String str,WXBizMsgCrypt wxBizMsgCrypt) throws Exception {
		return execute(null, str, wxBizMsgCrypt);
	}
	
	private String distribute(MessageBaseBean messageBaseBean,String str) throws Exception {
		return distribute(null, messageBaseBean, str);
	}
	
	private String distribute(String appid,MessageBaseBean messageBaseBean,String str) throws Exception {
		String result = "success";
		
		String msgType = messageBaseBean.getMsgType();
		
		if (msgType.equals(MessageBaseBean.Message_Event)) {
			EventMessageBean eventMessageBean = new EventMessageBean(str);
			eventMessageBean.transform();
			
			String respEvent = eventMessageBean.getEvent();
			String respEventKey = eventMessageBean.getEventKey();
			
			if (respEvent.equals(MessageBaseBean.Event_Subscribe)) {
				
				SubscribeEventBusiness subscribeEventBusiness = new SubscribeEventBusiness(appid,str);
				
				return subscribeEventBusiness.getSubscribeInfo(); 
			}
			else if (respEvent.equals(MessageBaseBean.Event_ScancodeWaitmsg)) {
				MenuScancodeWaitmsgBusiness menuScancodeWaitmsgBusiness = new MenuScancodeWaitmsgBusiness(appid,str);
				menuScancodeWaitmsgBusiness.bindCode();
				
				if (respEventKey.equals("33_receivablesCode")) {
					//暂时不做响应
				}
			}
			
			
		} else {
			if (msgType.equals(MessageBaseBean.Receive_Message_Text)) {
				ReceiveTextBusiness receiveTextBusiness = new ReceiveTextBusiness(appid,str);
				return receiveTextBusiness.getReplyText();
				
			}
		}
		
		return result;
		
	}

}
